// 准备饼图数据：统计每个label的总和
import {chartData} from "@/views/Test/components/Test3/lineChartData";

const preparePieData = () => {
    const labelMap = new Map<string, number>();

    chartData.forEach(item => {
        if (labelMap.has(item.label)) {
            labelMap.set(item.label, labelMap.get(item.label)! + 1);
        } else {
            labelMap.set(item.label, 1);
        }
    });

    return Array.from(labelMap.entries()).map(([name, value]) => ({
        name,
        value
    }));
};

// 准备饼图数据
const pieData = preparePieData();


// 饼图配置
export const pieChartOption: echarts.EChartsOption = {
    title: {
        text: '数据分布(饼图)',
        left: 'center'
    },
    tooltip: {
        trigger: 'item',
        formatter: '{a} <br/>{b}: {c} ({d}%)'
    },
    legend: {
        orient: 'vertical',
        left: 'left',
        data: pieData.map(item => item.name)
    },
    series: [
        {
            name: '数据分布',
            type: 'pie',
            radius: ['50%', '70%'],
            avoidLabelOverlap: false,
            itemStyle: {
                borderRadius: 10,
                borderColor: '#fff',
                borderWidth: 2
            },
            label: {
                show: false,
                position: 'center'
            },
            emphasis: {
                label: {
                    show: true,
                    fontSize: '18',
                    fontWeight: 'bold'
                }
            },
            labelLine: {
                show: false
            },
            data: pieData,
            color: ['#36a3f7', '#ff6a00', '#9b8bba', '#e7bcf3', '#ffd86e', '#a2cf6e']
        }
    ]
};